package com.onesports.intelligent.k12.polarlight.service.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.constant.CacheConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.DictDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.DictSelectDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Dict;
import com.onesports.intelligent.k12.polarlight.mapper.sys.DictMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 班级
 *
 * @author wusy
 * @since 1.0.0 2022-03-16
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.DICT, keyGenerator = "customCacheKeyGenerator")
public class SysDictService extends ServiceImpl<DictMapper, Dict> {
    private final DictMapper dictMapper;

    /**
     * 添加字典信息
     *
     * @param dictDTO
     */
    @CacheEvict(allEntries = true)
    public void add(DictDTO dictDTO) {
        //拿到dto对象，装入实体
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictDTO, dict);
        //进行排序判断
        //判断是否重复
        if (dictMapper.selectList(new QueryWrapper<Dict>().lambda()
                .eq(Dict::getParentId, dictDTO.getParentId())
                .eq(Dict::getSort, dictDTO.getSort())).size() > 0) {
            throw new BusinessException("排序号重复，请重新输入！");
        }
        //判断完成后加入数据库
        dictMapper.insert(dict);
    }

    /**
     * 修改字典信息
     *
     * @param dictDTO
     */
    @CacheEvict(allEntries = true)
    public void update(DictDTO dictDTO) {
        //拿到修改好的dto信息，装入实体
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictDTO, dict);
        //进行排序判断
        //先根据id取出排序号，如果相同则直接修改
        if (!dictMapper.selectById(dictDTO.getId()).getSort().equals(dictDTO.getSort())) {
            //不同判断id是否重复
            if (dictMapper.selectList(new QueryWrapper<Dict>().lambda()
                    .eq(Dict::getParentId, dictDTO.getParentId())
                    .eq(Dict::getSort, dictDTO.getSort())).size() > 0) {
                throw new BusinessException("排序号重复，请重新输入！");
            }
        }
        //判断完成后修改入数据库
        dictMapper.updateById(dict);
    }

    /**
     * 删除字典信息
     *
     * @param idDTO
     */
    @CacheEvict(allEntries = true)
    public void delete(IdDTO idDTO) {
        //进行删除
        dictMapper.deleteById(idDTO.getId());
    }

    /**
     * 条件查询
     *
     * @param dictSelectDTO
     */
    @Cacheable(cacheNames = CacheConstants.DICT)
    public List<Dict> select(DictSelectDTO dictSelectDTO) {
        return dictMapper.selectList(new QueryWrapper<Dict>().lambda()
                //查询所有大类
                .eq(Dict::getParentId, "0")
                //按照升序排序
                .orderByAsc(Dict::getSort)
                //按照大类名称进行模糊查询
                .like(ParamUtils.isNotEmpty(dictSelectDTO.getName()), Dict::getName, dictSelectDTO.getName())
                //按照大类值进行模糊查询
                .like(ParamUtils.isNotEmpty(dictSelectDTO.getCode()), Dict::getCode, dictSelectDTO.getCode()));
    }

    /**
     * 根据父级id对小类进行查询
     *
     * @param parentId
     * @return
     */
    public List<Dict> selectById(String parentId) {
        return dictMapper.selectList(new QueryWrapper<Dict>().lambda()
                //查询所对应的所有子类
                .eq(Dict::getParentId, parentId)
                //按照升序排序
                .orderByAsc(Dict::getSort));
    }

    /**
     * 排班模板表需要的字典数据
     * @return
     */
    public List<Dict> getByCode(){
        String sectionId = list(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, "section"))
                .stream()
                .map(Dict::getId)
                .findFirst().orElseThrow(()->new BusinessException("字典不存在"));
        return selectById(sectionId);
    }
}
